package com.weme.common.util

import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit

object TimeUtils {


    fun convertTimestampToRelativeTime(timestamp: Long): String {
        val now = System.currentTimeMillis()
        val diffMillis = now - timestamp

        // 一天的毫秒数
        val oneDayMillis = 24 * 60 * 60 * 1000

        val sdfToday = SimpleDateFormat("HH:mm", Locale.getDefault())
        val sdfYesterday = SimpleDateFormat("HH:mm", Locale.getDefault())
        sdfYesterday.timeZone = TimeZone.getTimeZone("UTC")
        val sdfOther = SimpleDateFormat("MM-dd", Locale.getDefault())
        sdfOther.timeZone = TimeZone.getTimeZone("UTC")

        return when {
            diffMillis < oneDayMillis -> { // 当日
                sdfToday.format(Date(timestamp))
            }
            diffMillis < 2 * oneDayMillis -> { // 昨日
                "Yesterday"
            }
            diffMillis < 365 * oneDayMillis -> { // 今年
                sdfOther.format(Date(timestamp))
            }
            else -> { // 更早
                "Long ago"
            }
        }
    }
    /**
     * 日期字符串(2022-06-16)转换为时间戳
     */
    fun dateStr2timeStamp(dateStr : String) : Long{

        val pattern = "yyyy-MM-dd"
        val simpleDateFormat = SimpleDateFormat(pattern)
        val date = simpleDateFormat.parse(dateStr)
        val timeStamp = date.time
        return timeStamp
    }

    /**
     * 将时间戳转为字符串日期
     * */
    fun timeStamp2DateStr(timeStamp: Long): String {
        val date = Date(timeStamp)
        val pattern = "yyyy-MM-dd" // 设定需要的日期格式
        val simpleDateFormat = SimpleDateFormat(pattern, Locale.getDefault()) // 使用默认的地区信息
        return simpleDateFormat.format(date)
    }



    fun formatTimestamp(timestamp: Long): String {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = timestamp

        // 获取当前日期
        val currentDate = Calendar.getInstance()
        val currentDayOfYear = currentDate.get(Calendar.DAY_OF_YEAR)
        val currentYear = currentDate.get(Calendar.YEAR)

        // 获取时间戳对应日期的年份和日期
        val dayOfYear = calendar.get(Calendar.DAY_OF_YEAR)
        val year = calendar.get(Calendar.YEAR)

        // 获取时间戳对应的小时和分钟
        val hourMinuteFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
        val formattedTime = hourMinuteFormat.format(calendar.time)

        // 判断是否是当日或昨日
        if (currentDayOfYear == dayOfYear && currentYear == year) {
            return formattedTime
        } else if (currentDayOfYear - dayOfYear == 1 && currentYear == year) { // 昨日
            return  String.format(UtilsApp.getTopActivity().getString(com.example.resources.R.string.yesterday,formattedTime))
        }

        // 获取月日和年份的格式化字符串
        val monthDayFormat = SimpleDateFormat("MM-dd HH:mm", Locale.getDefault())
        val yearFormat = SimpleDateFormat("yy-MM-dd HH:mm", Locale.getDefault())

        val formattedDate = if (year == currentYear) {
            monthDayFormat.format(calendar.time)
        } else {
            yearFormat.format(calendar.time)
        }

        return formattedDate
    }
    fun Long.isTimestampDifferenceGreaterThanTwoMinutes(otherTimestamp: Long): Boolean {
        // 计算两个时间戳的差值（毫秒为单位）
        val timeDifference = Math.abs(this - otherTimestamp)

        // 将差值转换为分钟
        val minutesDifference = TimeUnit.MILLISECONDS.toMinutes(timeDifference)

        // 如果相差大于等于两分钟，则返回 true
        return minutesDifference >= 2
    }
}